﻿

goog.provide('ol.dmp.interactionGeometry.AttackArrow');


goog.require('ol.dmp.interactionGeometry.Base');


ol.dmp.interactionGeometry.AttackArrow = function (options)
{
    this.maxPointsPerSketch_ = 4;
    this.minPointsPerSketch_ = 3;
    goog.base(this, options);
}

goog.inherits(ol.dmp.interactionGeometry.AttackArrow, ol.dmp.interactionGeometry.Base);


ol.dmp.interactionGeometry.AttackArrow.prototype.atFinish = function (event)
{
    if((this.keyPoints_.length + 1) == this.maxPointsPerSketch_)
    {
        return true;
    }

    return false;
};

ol.dmp.interactionGeometry.AttackArrow.prototype.startDrawing = function (event)
{
    var start = event.coordinate;
    this.finishCoordinate_ = start;
    this.keyPoints_.push(start);

    var polygonPoints = [this.keyPoints_.slice()];
    geometry = new ol.geom.Polygon(polygonPoints);
    this.sketchFeature_ = new ol.Feature();
    this.sketchFeature_.setGeometry(geometry);

    var linePoints = this.keyPoints_.slice();
    this.sketchLine_ = new ol.Feature(new ol.geom.LineString(linePoints));

};

ol.dmp.interactionGeometry.AttackArrow.prototype.modifyDrawing = function (event)
{
    var coordinate = event.coordinate;
    var geometry = this.sketchFeature_.getGeometry();
    var sketchLineGeom = this.sketchLine_.getGeometry();
    var keypts = this.keyPoints_.slice();
    keypts.push(coordinate);

    var polygonPoints = keypts.slice();

    if (keypts.length == this.maxPointsPerSketch_ || keypts.length == this.minPointsPerSketch_)
    {
        polygonPoints = ol.dmp.algorithmsUtility.attackArrow(keypts)
    }
    sketchLineGeom.setCoordinates(polygonPoints);
    geometry.setCoordinates([polygonPoints]);
};

ol.dmp.interactionGeometry.AttackArrow.prototype.addToDrawing = function (event)
{
    var coordinate = event.coordinate;
    var geometry = this.sketchFeature_.getGeometry();

    this.keyPoints_.push(coordinate);
    var keypts = this.keyPoints_.slice();

    var polygonPoints = keypts.slice();
    if (keypts.length == this.maxPointsPerSketch_ || keypts.length == this.minPointsPerSketch_)
    {
        polygonPoints = ol.dmp.algorithmsUtility.attackArrow(keypts)
    }
    geometry.setCoordinates([polygonPoints]);
};



